<?php
//
// 分销对账
//
class Controller_Admin_ShareShop extends Controller_Form
{
    public function __construct ($req, $res)
    {
        $this->_form_type  = 'shareshop';
        $this->_no_buttons = TRUE;
        parent::__construct($req, $res);

        $this->_list_where = array(
            array('status', '=',  1),
        );

        $this->_sort_extra_columns = array(
            array('changed', DB::expr('curdate()')),
        );

        $this->_shopId = App_Shop_Manager::currentShopId();
    }

	//默认首页
    public function action_index ()
    {
        $this->action_list();
    }

    // 对账单列表
    public function action_list ($page = 1)
    {
        //
        $query    = $this->request->query();
        $startDay = Arr::get($query, 'startDay', date("Y-m-d", time() - 86400 * 30));
        $stopDay  = Arr::get($query, 'stopDay',  date("Y-m-d", time()));
        $shopId   = Arr::get($query, 'shopId', $this->_shopId);
        if ($this->_shopId > 0 && $this->_shopId != $shopId)
            $shopId = $this->_shopId;

        $filter   = array(
            'shopId' => $shopId,
            'startDay' => $startDay,
            'stopDay'  => $stopDay,
            'shopList' => App_Shop::options_shopNames(),
        );

        $page     = max(1, $page);
        $limit    = 500;
        $offset   = $limit * ($page - 1);

        list($counts, $records, $moneyShopTotal) = App_Share_Money::paymentList($startDay, $stopDay, $shopId, $offset, $limit);
        $pagination = $this->pagination($counts, $limit);

        $this->template->set('counts', $counts);
        $this->template->set('records', $records);
        $this->template->set('moneyShopTotal', $moneyShopTotal);
        $this->template->set('filter', $filter);
        $this->template->set('pagination', $pagination);
        $this->template->set_filename('smarty:admin/share/payment_list');
    }

    public function action_sharedMoney($orderNo)
    {
        $records = App_Share_Money::paymentSharedMoneyList($orderNo);
        $this->template->set('records', $records);
        $this->template->set('orderNo', $orderNo);
        $this->template->set_filename('smarty:admin/share/payment_sharedMoney');
    }

	//添加分享家
    public function action_create ()
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;
        }

        parent::action_create();
    }

	//修改分享家
    public function action_update ($nid)
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $this->rebuild_post_data($post);
            $this->_form_data = $post;
        }
        else
        {
            $data = Form_Data::detail($this->_form_type, $nid); 
            $this->_form_data = $data;
        }

        parent::action_update($nid);
    }

	// 详情
	//$nid int 商铺ID
    public function action_detail ($nid)
    {
        $data = Form_Data::detail($this->_form_type, $nid); 

        $this->RMBFen2Yuan($data['money']);
        $this->RMBFen2Yuan($data['moneyIncome']);
        $this->RMBFen2Yuan($data['money2Cash']);
        $this->RMBFen2Yuan($data['money2Coin']);
        $data['created'] = date("Y-m-d H:i:s", $data['created']);

        $this->_form_data = $data;

        parent::action_detail($nid);
    }

	// 删除分享家
    public function action_delete ($nid)
    {
        App_Deliver::remove($nid);
        $this->to_url('list');
    }

    public function detail_filter_config_data (& $arr_config)
    {
        //
    }

	//重构数据
    private function rebuild_post_data (& $post)
    {
        //
    }

	// 列表数据改造
    public function list_filter_data (& $rows)
    {
        foreach ($rows as & $rs)
        {
            //
            $this->RMBFen2Yuan($rs['money']);
            $this->RMBFen2Yuan($rs['moneyIncome']);
            $this->RMBFen2Yuan($rs['money2Cash']);
            $this->RMBFen2Yuan($rs['money2Coin']);
        }
    }

	//操作菜单
    public function actions_by_list_row (& $row, & $menus)
    {
        //
    }
}
